package com.newvpn.fastbest.zenith.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.newvpn.fastbest.zenith.VPNApp;


public abstract class BaseDialog extends DialogFragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        //VPNApp.setInApp(true);
        // 使用AlertDialog.Builder创建对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // 获取布局填充器
        LayoutInflater inflater = getActivity().getLayoutInflater();
        // 载入自定义布局
        View view = inflater.inflate(layout(), null);
        builder.setView(view);
        init(view);
        Dialog dialog = builder.create();
//        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//        // 设置对话框的左右边距
//        Window window = dialog.getWindow();
//        if (window != null) {
//            WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
//            layoutParams.copyFrom(window.getAttributes());
//            layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
//            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
//            layoutParams.gravity = Gravity.CENTER;
//            layoutParams.horizontalMargin = 500;
//            window.setAttributes(layoutParams);
//        }
        return dialog;
    }

    protected abstract void init(View view);

    protected abstract int layout();


}

